/******************************************************************************************
* Project: Workhorses of Opportunity — Howard and Weinstein
*
* Inputs:
*   - "TFS_pub_ipeds_merge_12Dec.dta"
*
* Outputs:
*   - "tfs_cleaned.dta" (cleaned dataset)
******************************************************************************************/

use "TFS_pub_ipeds_merge_12Dec.dta", clear
keep if home_hasnormalschool~=.

//order unitid year homezip homectyfips homestate home_hasnormalschool

//collapse (mean) totstud-c_safety (count) numobs=unitid, by(year homezip homectyfips homestate home_hasnormalschool)


*Differences in reasons for choice of university

gen r_job = reason05
replace r_job = reason04_t if year>=1985 & year<=1988 & r_job==.

gen r_money = reason06
gen r_training = reason07
gen r_mentor = reason01

gen r_nojob = reason02
replace r_nojob = reason02_t if year>=1985 & year<=1988 & r_nojob==.

gen r_parents = reason03
replace r_parents = reason03_t if year>=1985 & year<=1988 & r_parents==.

gen r_nothingbet = reason04
gen r_gened = reason08
gen r_reading = reason09
gen r_learnint = reason10
gen r_culture = reason11
gen r_gradsch = reason12
gen r_awayhome = reason13
replace r_awayhome = reason05_t if year>=1985 & year<=1988 & r_awayhome==.

gen r_fam = reason14
gen r_newpeople = reason01_t
gen r_prove = reason06_t
gen r_findpurp = reason07_t
gen r_contrib = reason01_n
gen r_friendsgoing = reason03_n

gen c_nearhome = choose09
gen c_goodjobs = choose29
gen c_collegerep = choose01
gen c_friendsugg = choose02
gen c_visit = choose03
gen c_online = choose04
gen c_notafffirst = choose05

gen c_hscouns = choose06
replace c_hscouns = choose14 if c_hscouns==.


gen c_size = choose07
gen c_liveathome = choose08
gen c_early = choose10
gen c_relig = choose11
gen c_offeredaid = choose12
gen c_websiteinf = choose13
gen c_guidcouns = choose14
gen c_parents = choose15
gen c_relatives = choose16
gen c_teacher = choose17
gen c_onlyaccept = choose18
gen c_notaidfirst = choose19
gen c_prvtcouns = choose20
gen c_rank = choose21
gen c_recrathl = choose22
gen c_cost = choose23
gen c_social = choose24
gen c_acadreput = choose25
gen c_lowtuit = choose26
gen c_speced = choose27
gen c_topgrad = choose28
gen c_gradrate = choose30
gen c_parentsrel = choose31
gen c_socextra = choose32
gen c_makediff = choose33
gen c_communicprof = choose34
gen c_reputmajor = choose35
gen c_liveawayhome = choose01_t
gen c_meritaid = choose02_t
gen c_needaid = choose03_t
gen c_athsch = choose04_t
gen c_guide = choose05_t
gen c_friendsgoing = choose06_t
gen c_friendsatt = choose07_t 
gen c_prevstud = choose08_t
gen c_race = choose91_n
gen c_locnoothoption = choose93_n
gen c_safety = choose0202_n

gen f_job = futact12
gen f_prot = futact24
gen f_ftjob = futact33
gen f_outsidejob = futact08_t
gen f_transf = futact32

gen a_fam = aid1>=2
replace a_fam = . if aid1==.

gen a_own = aid2>=2
replace a_own = . if aid2==.

gen a_gr = aid3>=2
replace a_gr = . if aid3==.

gen a_loan = aid4>=2
replace a_loan = . if aid4==.

gen a_oth = aid5>=2
replace a_oth = . if aid5==.

gen a_famfr = aid01>=2
replace a_famfr = . if aid01==.

gen a_spouse = aid02>=2
replace a_spouse = . if aid02==.

gen a_summsav = aid03>=2
replace a_summsav = . if aid03==.

gen a_othsav = aid04>=2
replace a_othsav = . if aid04==.

gen a_ptjobc = aid05>=2
replace a_ptjobc = . if aid05==.

gen a_ptjoboffc = aid06>=2
replace a_ptjoboffc = . if aid06==.

gen a_ftjobincoll = aid07>=2
replace a_ftjobincoll = . if aid07==.

gen a_pell = aid08>=2
replace a_pell = . if aid08==.

gen a_seogr = aid09>=2
replace a_seogr = . if aid09==.

gen a_stgr = aid10>=2
replace a_stgr = . if aid10==.

gen a_wsgr = aid11>=2
replace a_wsgr = . if aid11==.

gen a_collgroth = aid12>=2
replace a_collgroth = . if aid12==.

gen a_vocfund = aid13>=2
replace a_vocfund = . if aid13==.

gen a_othprgr = aid14>=2
replace a_othprgr = . if aid14==.

gen a_othgov = aid15>=2
replace a_othgov = . if aid15==.

gen a_stafford = aid16>=2
replace a_stafford = . if aid16==.

gen a_perkins = aid17>=2
replace a_perkins = . if aid17==.

gen a_othcollloan = aid18>=2
replace a_othcollloan = . if aid18==.

gen a_othloan = aid19>=2
replace a_othloan = . if aid19==.

gen a_oth2 = aid20>=2
replace a_oth2 = . if aid20==.


gen a_fam_c = a_fam
replace a_fam_c = max(a_famfr, a_spouse) if a_fam==.

gen a_own_c = a_own
replace a_own_c = max(a_summsav, a_othsav, a_ptjobc, a_ptjoboffc, a_ftjobincoll) if a_own==.

gen a_gr_c = a_gr
replace a_gr_c = max(a_pell, a_seogr, a_stgr, a_wsgr, a_collgroth, a_vocfund, a_othprgr, a_othprgr) if a_gr==.

gen a_loan_c = a_loan
replace a_loan_c = max(a_stafford, a_perkins, a_othcollloan, a_othloan) if a_loan==.

gen a_oth_c = a_oth
replace a_oth_c = a_oth2 if a_oth==.

gen a_othcoll82_y = aidothcol82_n==3
replace a_othcoll82_y = . if aidothcol82_n==.

gen a_othcoll82_m = aidothcol82_n>=2
replace a_othcoll82_m = . if aidothcol82_n==.

gen firstchoice = choice==5
replace firstchoice = . if choice==.


#delimit ;
local vars r_job r_money r_training r_mentor r_nojob r_parents r_nothingbet
r_gened r_reading r_learnint r_culture r_gradsch r_awayhome r_fam r_newpeople
r_prove r_findpurp r_contrib r_friendsgoing 
c_nearhome c_goodjobs c_collegerep c_friendsugg c_visit c_online c_notafffirst 
c_hscouns c_size c_liveathome c_early c_relig c_offeredaid c_websiteinf c_guidcouns
c_parents c_relatives c_teacher c_onlyaccept c_notaidfirst c_prvtcouns c_rank
c_recrathl c_cost c_social c_acadreput c_lowtuit c_speced c_topgrad c_gradrate
c_parentsrel c_socextra c_makediff c_communicprof c_reputmajor c_liveawayhome 
c_meritaid c_needaid c_athsch c_guide c_friendsgoing c_friendsatt  c_prevstud
c_race c_locnoothoption c_safety fincon;

#delimit cr
foreach x of local vars{

gen `x'v = `x'==3
replace `x'v = . if `x'==0
replace `x'v = . if `x'==.
//
// gen normin10`x'v = `x'v==1 & attendnormwithin10==1
// replace normin10`x'v=. if attendnormwithin10==.|`x'v==.

gen `x's = `x'>=2
replace `x's = . if `x'==0
replace `x's = . if `x'==.

// gen normin10`x's = `x's==1 & attendnormwithin10==1
// replace normin10`x's=. if attendnormwithin10==.|`x's==.

}

local vars f_job  f_prot  f_ftjob  f_outsidejob f_transf
#delimit cr
foreach x of local vars{
gen `x'v = `x'==4
replace `x'v = . if `x'==0
replace `x'v = . if `x'==.

// gen normin10`x'v = `x'v==1 & attendnormwithin10==1
// replace normin10`x'v=. if attendnormwithin10==.|`x'v==.

gen `x'U = `x'
replace `x'U = . if `x'U==0

gen `x's = `x'>=3
replace `x's = . if `x'==0
replace `x's = . if `x'==.

// gen normin10`x's = `x's==1 & attendnormwithin10==1
// replace normin10`x's=. if attendnormwithin10==.|`x's==.
}

egen homestateyrgr = group(homestate year)

gen numapplyone = numapply==1
replace numapplyone = . if numapply==.

gen planlivefam = planlive==1
replace planlivefam = . if planlive==.

gen planlivefamprefoth = planlive==1 & preflive~=1
replace planlivefamprefoth = . if planlive==.|preflive==.

gen planlivefampreffam = planlive==1 & preflive==1
replace planlivefampreffam = . if planlive==.|preflive==.

gen fathatlba = fatheduc>=6
replace fathatlba = . if fatheduc==.

gen fathhsgradorless = fatheduc<=3
replace fathhsgradorless = . if fatheduc==.


gen momatlba = motheduc>=6
replace momatlba = . if motheduc==.

gen momhsgradorless = motheduc<=3
replace momhsgradorless = . if motheduc==.

gen momanddadatlba = fathatlba==1 & momatlba==1
replace momanddadatlba = . if fathatlba==. & momatlba==.

gen momandadhsorless = fathhsgradorless==1 & momhsgradorless==1
replace momandadhsorless = . if fathhsgradorless==. & momhsgradorless==.


local vars fatheduc motheduc
foreach x of local vars{
gen `x'yrs = 8 if `x'==1
replace `x'yrs = 10 if `x'==2
replace `x'yrs = 12 if `x'==3
replace `x'yrs = 13 if `x'==4
replace `x'yrs = 14 if `x'==5
replace `x'yrs = 16 if `x'==6
replace `x'yrs = 17 if `x'==7
replace `x'yrs = 18 if `x'==8
}

gen white = racegroup==5
replace white = . if racegroup==.

gen black = racegroup==3
replace black = . if racegroup==.

tab fatheduc, gen(fatheduc)
tab motheduc, gen(motheduc)

gen male = sex==1
replace male=. if sex==.

gen ageUSE = 16 if age1==1
replace ageUSE = 17 if age1==2
replace ageUSE = 18 if age1==3
replace ageUSE = 19 if age1==4
replace ageUSE = 20 if age1==5
replace ageUSE = 23 if age1==6
replace ageUSE = 27.5 if age1==7
replace ageUSE = 35 if age1==8
replace ageUSE = 47.5 if age1==9
replace ageUSE = 55 if age1==10

gen actcompUSE = actcomp
replace actcompUSE = . if actcomp==99

gen lib = poliview>=4
replace lib = . if poliview==.

gen income_midpoint = 3000 if income==1
replace income_midpoint = 8000 if income==2
replace income_midpoint = 5000 if income==3
replace income_midpoint = 12500 if income==4
replace income_midpoint = 17500 if income==6
replace income_midpoint = 22500 if income==8
replace income_midpoint = 27500 if income==9
replace income_midpoint = 32500 if income==11
replace income_midpoint = 35000 if income==12
replace income_midpoint = 37500 if income==14
replace income_midpoint = 45000 if income==16
replace income_midpoint = 55000 if income==18
replace income_midpoint = 75000 if income==19
replace income_midpoint = 67500 if income==20
replace income_midpoint = 87500 if income==21
replace income_midpoint = 100000 if income==22
replace income_midpoint = 125000 if income==24
replace income_midpoint = 150000 if income==26
replace income_midpoint = 175000 if income==27
replace income_midpoint = 200000 if income==28
replace income_midpoint = 225000 if income==29
replace income_midpoint = 250000 if income==30

gen sattot = satm + satv
replace sattot = . if satm==. | satv==.

gen lninc_midpoint = ln(income_midpoint)


#delimit ;
local vars fatheduc1 fatheduc2 fatheduc3 fatheduc4 
fatheduc5 fatheduc6 fatheduc7 fatheduc8 motheduc1 motheduc2 motheduc3 motheduc4 motheduc5 motheduc6 motheduc7 
motheduc8 white male ageUSE actcompUSE 
planlivefam planlivefamprefoth planlivefampreffam numapply numapplyone  
satm satv sattot  f_job  f_prot  f_ftjob  f_outsidejob a_fam a_own a_gr a_loan a_oth a_famfr a_spouse a_summsav a_othsav a_ptjobc 
a_ptjoboffc a_ftjobincoll a_pell a_seogr a_stgr a_wsgr a_collgroth a_vocfund  
a_othprgr a_othgov a_stafford a_perkins a_othcollloan a_othloan a_oth2 poliview lib fincon income_midpoint lninc_midpoint rate01;
#delimit cr

// foreach x of local vars{
// gen normin10`x' = `x'==1 & attendnormwithin10==1
// replace normin10`x'=. if attendnormwithin10==.|`x'==.
// }


* Save cleaned dataset
save "tfs_cleaned", replace
